using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Collections;
namespace DAO
{
    public class LoaiHangDAO
    {
        #region DanhSach
        public IList DanhSach()
        {
            SqlConnection connect = SqlDataAccessHelper.ConnectSQL();
            SqlCommand command = connect.CreateCommand();
            command.CommandText = "SELECT * FROM LOAIHANG";
            SqlDataReader dr = command.ExecuteReader();
            IList list = new ArrayList();
            while(dr.Read())
            {
                LoaiHangDTO loaihang = new LoaiHangDTO();
                loaihang.MaLoaiHang = (int)dr["MaLoaiHang"];
                loaihang.TenLoaiHang = (string)dr["TenLoaiHang"];
                list.Add(loaihang);
            }
            dr.Close();
            connect.Close();
            LoaiHangDTO[] listLoaiHang = new LoaiHangDTO[list.Count];
            list.CopyTo(listLoaiHang, 0);
            return listLoaiHang;
        }

        #endregion
        #region Retrieving
        public static List <LoaiHangDTO> SelectLoaiHangAll()
        {
            List<LoaiHangDTO> list = new List<LoaiHangDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spSelectLoaiHangAll");
                foreach(DataRow dr in dt.Rows)
                {
                    LoaiHangDTO loaihang = new LoaiHangDTO();
                    loaihang.MaLoaiHang = int.Parse(dr["MaLoaiHang"].ToString());
                    loaihang.TenLoaiHang = dr["TenLoaiHang"].ToString();
                    list.Add(loaihang);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static LoaiHangDTO SelectLoaiHangByMa(int MaLoaiHang)
        {
            LoaiHangDTO loaihang = new LoaiHangDTO();
            try
            {
                //Create list Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MALOAI", MaLoaiHang));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spSelectLoaiHangByID", sqlParams);
                DataRow dr = dt.Rows[0];
                loaihang.MaLoaiHang = int.Parse(dr["MaLoaiHang"].ToString());
                loaihang.TenLoaiHang = dr["TenLoaiHang"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return loaihang;
        }

        public static bool CheckLoaiHangByMa(int MaLoaiHang)
        {
            bool result = false;
            try
            {
                //Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MALOAI", MaLoaiHang));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spSelectLoaiHangByID", sqlParams);
                if(dt.Rows.Count == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
    }
}
